<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();

$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];

$db = NEW myDBS;
$db->selectDB("hr_udoncity");
$sql = "SELECT person.id, fname, lname, position_name "
        . " FROM person, position "
        . " WHERE position.id = person.id ";

if ($panngan2_id) {
    $sql = $sql . " AND panngan2_id = '$panngan2_id' ";

    if ($panngan3_id) {
        $sql = $sql . " AND panngan3_id = '$panngan3_id' ";
    }
}

//echo $sql;
$query = $db->runSQL($sql);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#panngan2_id").change(function() {
            var panngan2 = $(this).val();
            var panngan3 = "<?php echo $panngan3_id; ?>";

            if (panngan2 != "") {
                //alert(panngan2);
                $.post('dd_panngan_1.php', {panngan2: panngan2, panngan3: panngan3}, function(data) {
                    $("#dd_panngan3").html(data);

                });
            }
        });

        $('#panngan2_id').trigger('change');

        $("#payment_method").change(function() {
            if ($(this).val() != "cash") {
                //alert("adsf");
                var payment_method = $(this).val();
                var card_id =
                        $('<div>').dialog2({
                    autoOpen: true,
                    title: "เลือกการจ่ายเงิน",
                    content: "payment_edit.php?card_id=" + card_id + "&payment_method=" + payment_method,
                    id: "modal-payment"
                });
            }
        });

    });

    function payment(card_id) {
        //alert(card_id);
        var panngan2_id = "<?php echo $panngan2_id; ?>";
        var panngan3_id = "<?php echo $panngan3_id; ?>";

        $('<div>').dialog2({
            autoOpen: true,
            title: "เลือกการจ่ายเงิน",
            content: "payment_edit.php?card_id=" + card_id + "&panngan2_id=" + panngan2_id + "&panngan3_id=" + panngan3_id,
            id: "modal-payment"
        });
    }
</script>
<div class="row-fluid">
    <form action="" method="post">
        <div class="row-fluid">
            <div class="control-group span4">
                <label class="control-label" for="panngan">แผนงาน</label>
                <div class="controls ">
                    <select class="span12" id="panngan2_id" name="panngan2_id" required>
                        <option value="">เลือก...</option>
                        <?php
                        $sql = "SELECT DISTINCT panngan2_id 
                            FROM position 
                            WHERE lvl_id like '" . $_SESSION["lvl1_id"] . "%'
                            AND panngan2_id IS NOT NULL"
                        ;

                        $result = $db->runSQL($sql);

                        while ($rows = $db->fetch_object($result)) {
                            $db->selectDB("account");
                            $sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" . $rows->panngan2_id . "'";
                            $result1 = $db->runSQL($sql);
                            while ($rows1 = $db->fetch_object($result1)) {
                                if ($rows1->panngan2_id == $panngan2_id) {
                                    $selected = " selected";
                                } else {
                                    $selected = "";
                                }
                                echo "<option value='" . $rows1->panngan2_id . "' $selected>$rows1->panngan2_name</option>";
                            }
                        }
                        ?>
                    </select>
                </div>
            </div><!--/span-->

            <div class="control-group span4">
                <label class="control-label" for="panngan3">งาน</label>
                <div class="controls " id="dd_panngan3">
                    <select class="span12" id="panngan3_id" name="panngan3_id">
                        <option value="">เลือก</option>

                    </select>
                </div>
            </div>
            <div class="control-group span4">
                <label class="control-label" for="">&nbsp;</label>
                <button type="submit" class="btn btn-info" id='OK' name='OK'><i class="icon-ok icon-white"></i> ตกลง</button>
            </div>
        </div>
        <input type='hidden' id='pages' name='pages' value="person_payment">
    </form>
</div>

<div class="row-fluid">
    <table class="table table-bordered table-hover" id='person'>
        <thead>
        <th>#</th>
        <th>ชื่อ นามสกุล</th>
        <th>ตำแหน่ง</th>
        <th>เลือกการจ่ายเงิน</th>
        </thead>
        <tbody>
            <?php
            $i = 1;
            while ($rows = $db->fetch_object($query)) {
                ?>
                <tr>
                    <td><p class="text-center"><?php echo $i; ?></p></td>
                    <td>&nbsp;<?php echo $rows->fname . "&nbsp;&nbsp;&nbsp;" . $rows->lname; ?></td>
                    <td>&nbsp;<?php echo $rows->position_name; ?></td>
                    <td>&nbsp;
                        <?php
                        $sql = "SELECT * "
                                . " FROM tbl_payment "
                                . " WHERE card_id = '$rows->id' ";
                        $row = $db->fetch_object($db->runSQL($sql));
                        ?>
                        <?php echo $row->payment_method; ?>&nbsp;<a href="javascript:payment(<?php echo $rows->id; ?>)" class="btn btn-success">เลือก</a>

            <!--                        <select id='payment_method' name='payment_method'>
                <option value='cash'>เงินสด</option>
                <option value='cheque'>เช็ค</option>
                <option value='trans'>เงินโอน</option>
            </select>-->
                    </td>
                </tr>
                <?php
                $i++;
            }
            ?>
        </tbody>
    </table>

</div>



